package com.geek.factory.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.geek.factory.constant.SuccessConstant;
import com.geek.factory.entity.Repair;
import com.geek.factory.result.Result;
import com.geek.factory.service.RepairService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/dev/repair")
@Api(tags = "设备维修管理")
@Slf4j
public class RepairController {

    @Autowired
    private RepairService repairService;

    @PostMapping("/create")
    @ApiOperation("新增维修记录")
    public Result createRepair(@RequestBody Repair repair) {
        log.info("新增维修记录, 参数: {}", repair);
        try {
            repairService.save(repair);
            return new Result(SuccessConstant.SUCCESS, "新增成功");
        } catch (Exception e) {
            log.error("新增维修记录失败", e);
            return new Result(SuccessConstant.FAIL, "新增失败");
        }
    }

    @GetMapping("/{id}")
    @ApiOperation("根据ID查询维修记录")
    public Result getRepairById(@PathVariable Integer id) {
        log.info("根据ID查询维修记录, id: {}", id);
        try {
            Repair repair = repairService.getById(id);
            return new Result(SuccessConstant.SUCCESS, "查询成功", repair);
        } catch (Exception e) {
            log.error("查询维修记录失败", e);
            return new Result(SuccessConstant.FAIL, "查询失败");
        }
    }

    @PutMapping("/update")
    @ApiOperation("修改维修记录")
    public Result updateRepair(@RequestBody Repair repair) {
        log.info("修改维修记录, 参数: {}", repair);
        try {
            repairService.updateById(repair);
            return new Result(SuccessConstant.SUCCESS, "修改成功");
        } catch (Exception e) {
            log.error("修改维修记录失败", e);
            return new Result(SuccessConstant.FAIL, "修改失败");
        }
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation("根据ID删除维修记录")
    public Result deleteRepair(@PathVariable Integer id) {
        log.info("根据ID删除维修记录, id: {}", id);
        try {
            repairService.removeById(id);
            return new Result(SuccessConstant.SUCCESS, "删除成功");
        } catch (Exception e) {
            log.error("删除维修记录失败", e);
            return new Result(SuccessConstant.FAIL, "删除失败");
        }
    }

    @PostMapping("/list")
    @ApiOperation("分页查询维修记录")
    public Result getRepairList(@RequestBody(required = false) Map<String, Object> params) {
        log.info("分页查询维修记录 - 参数: {}", params);

        try {
            Integer current = params != null && params.get("current") != null ?
                    Integer.parseInt(params.get("current").toString()) : 1;
            Integer size = params != null && params.get("size") != null ?
                    Integer.parseInt(params.get("size").toString()) : 10;

            Page<Repair> page = new Page<>(current, size);
            LambdaQueryWrapper<Repair> wrapper = new LambdaQueryWrapper<>();

            if (params != null) {
                if (params.get("deviceName") != null && StringUtils.isNotBlank(params.get("deviceName").toString())) {
                    wrapper.like(Repair::getDeviceName, params.get("deviceName").toString());
                }
                if (params.get("deviceId") != null && StringUtils.isNotBlank(params.get("deviceId").toString())) {
                    wrapper.like(Repair::getDeviceId, params.get("deviceId").toString());
                }
                if (params.get("status") != null && StringUtils.isNotBlank(params.get("status").toString())) {
                    wrapper.eq(Repair::getStatus, params.get("status").toString());
                }
                if (params.get("productlineId") != null) {
                    wrapper.eq(Repair::getProductlineId, params.get("productlineId"));
                }
            }

            Page<Repair> resultPage = repairService.page(page, wrapper);

            Map<String, Object> result = new HashMap<>();
            result.put("records", resultPage.getRecords());
            result.put("total", resultPage.getTotal());
            result.put("current", resultPage.getCurrent());
            result.put("size", resultPage.getSize());

            return new Result(SuccessConstant.SUCCESS, "查询成功", result);
        } catch (Exception e) {
            log.error("查询维修记录失败", e);
            return new Result(SuccessConstant.FAIL, "查询失败");
        }
    }










}
